package com.bootlever.framework.common.utils;

import java.io.File;
import java.util.Iterator;
import java.util.Random;

public final class StringUtils {

    private StringUtils() {
    }

    /**
     * 字符串截取数字
     * @param src
     * @param start
     * @param to
     * @return
     */
    public static Integer subStringToInteger(String src, String start, String to) {
        return stringToInteger(subString(src, start, to));
    }

    /**
     * 子字符串
     * @param src
     * @param start
     * @param to
     * @return
     */
    public static String subString(String src, String start, String to) {
        int indexFrom = start == null ? 0 : src.indexOf(start);
        int indexTo = to == null ? src.length() : src.indexOf(to);
        if (indexFrom >= 0 && indexTo >= 0 && indexFrom <= indexTo) {
            if (null != start) {
                indexFrom += start.length();
            }

            return src.substring(indexFrom, indexTo);
        } else {
            return null;
        }
    }

    /**
     * 子字符串
     * @param src
     * @param start
     * @param to
     * @param toLast
     * @return
     */
    public static String subString(String src, String start, String to, boolean toLast) {
        if (!toLast) {
            return subString(src, start, to);
        } else {
            int indexFrom = start == null ? 0 : src.indexOf(start);
            int indexTo = to == null ? src.length() : src.lastIndexOf(to);
            if (indexFrom >= 0 && indexTo >= 0 && indexFrom <= indexTo) {
                if (null != start) {
                    indexFrom += start.length();
                }

                return src.substring(indexFrom, indexTo);
            } else {
                return null;
            }
        }
    }

    /**
     * 截取字符串
     * @param str
     * @param start
     * @param length
     * @return
     */
    public static String subString(String str, int start,int end) {
        if (isEmpty(str)) {
            return "";
        }
        return str.substring(start,end);
    }
    /**
     * 字符串转整型
     * @param in
     * @return
     */
    public static Integer stringToInteger(String in) {
        if (in == null) {
            return null;
        } else {
            in = in.trim();
            if (in.length() == 0) {
                return null;
            } else {
                return Integer.parseInt(in);
            }
        }
    }

    /**
     * 是否相同
     * @param a
     * @param b
     * @return
     */
    public static boolean equals(String a, String b) {
        if (a == null) {
            return b == null;
        } else {
            return a.equals(b);
        }
    }

    /**
     * 是否大小写忽略后相同
     * @param a
     * @param b
     * @return
     */
    public static boolean equalsIgnoreCase(String a, String b) {
        if (a == null) {
            return b == null;
        } else {
            return a.equalsIgnoreCase(b);
        }
    }

    /**
     * 是否空字符串
     * @param value
     * @return
     */
    public static boolean isEmpty(CharSequence value) {
        return value == null || value.length() == 0;
    }

    /**
     * 是否为空字符串
     * @return
     */
    public static boolean isEmpty(String string){
        return string == null || string.length() == 0;
    }

    /**
     * 是否非空
     * @return
     */
    public static boolean isNotEmpty(String str) {
        return !(isEmpty(str));
    }

    /**
     * 结果转字符串
     * @return
     */
    public static String getNString(Object object){
        return object == null ? "" : object.toString().trim();
    }
    public static int lowerHashCode(String text) {
        if (text == null) {
            return 0;
        } else {
            int h = 0;

            for(int i = 0; i < text.length(); ++i) {
                char ch = text.charAt(i);
                if (ch >= 'A' && ch <= 'Z') {
                    ch = (char)(ch + 32);
                }

                h = 31 * h + ch;
            }

            return h;
        }
    }

    /**
     * 是否是数字
     * @param str
     * @return
     */
    public static boolean isNumber(String str) {
        if (isEmpty(str)) {
            return false;
        } else {
            char[] chars = str.toCharArray();
            int sz = chars.length;
            boolean hasExp = false;
            boolean hasDecPoint = false;
            boolean allowSigns = false;
            boolean foundDigit = false;
            int start = chars[0] == '-' ? 1 : 0;
            int i;
            if (sz > start + 1 && chars[start] == '0' && chars[start + 1] == 'x') {
                i = start + 2;
                if (i == sz) {
                    return false;
                } else {
                    while(i < chars.length) {
                        if ((chars[i] < '0' || chars[i] > '9') && (chars[i] < 'a' || chars[i] > 'f') && (chars[i] < 'A' || chars[i] > 'F')) {
                            return false;
                        }

                        ++i;
                    }

                    return true;
                }
            } else {
                --sz;

                for(i = start; i < sz || i < sz + 1 && allowSigns && !foundDigit; ++i) {
                    if (chars[i] >= '0' && chars[i] <= '9') {
                        foundDigit = true;
                        allowSigns = false;
                    } else if (chars[i] == '.') {
                        if (hasDecPoint || hasExp) {
                            return false;
                        }

                        hasDecPoint = true;
                    } else if (chars[i] != 'e' && chars[i] != 'E') {
                        if (chars[i] != '+' && chars[i] != '-') {
                            return false;
                        }

                        if (!allowSigns) {
                            return false;
                        }

                        allowSigns = false;
                        foundDigit = false;
                    } else {
                        if (hasExp) {
                            return false;
                        }

                        if (!foundDigit) {
                            return false;
                        }

                        hasExp = true;
                        allowSigns = true;
                    }
                }

                if (i < chars.length) {
                    if (chars[i] >= '0' && chars[i] <= '9') {
                        return true;
                    } else if (chars[i] != 'e' && chars[i] != 'E') {
                        if (!allowSigns && (chars[i] == 'd' || chars[i] == 'D' || chars[i] == 'f' || chars[i] == 'F')) {
                            return foundDigit;
                        } else if (chars[i] != 'l' && chars[i] != 'L') {
                            return false;
                        } else {
                            return foundDigit && !hasExp;
                        }
                    } else {
                        return false;
                    }
                } else {
                    return !allowSigns && foundDigit;
                }
            }
        }
    }

    /**
     * 获取随机字符串
     * @param length
     * @return
     */
    public static String getRandonStr(int length) {
        String str="zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        //由Random生成随机数
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        //长度为几就循环几次
        for(int i=0; i<length; ++i){
            //产生0-61的数字
            int number=random.nextInt(62);
            //将产生的数字通过length次承载到sb中
            sb.append(str.charAt(number));
        }
        //将承载的字符转换成字符串
        return sb.toString();
    }

    /**
     * 集合转字符串
     * @param iterator
     * @param separator
     * @return
     */
    public static String join(Iterator iterator, String separator) {

        // handle null, zero and one elements before building a buffer
        if (iterator == null) {
            return null;
        }
        if (!iterator.hasNext()) {
            return "";
        }
        Object first = iterator.next();
        if (!iterator.hasNext()) {
            return StringUtils.getNString(first);
        }

        // two or more elements
        StringBuffer buf = new StringBuffer(256); // Java default is 16, probably too small
        if (first != null) {
            buf.append(first);
        }

        while (iterator.hasNext()) {
            if (separator != null) {
                buf.append(separator);
            }
            Object obj = iterator.next();
            if (obj != null) {
                buf.append(obj);
            }
        }
        return buf.toString();
    }

    /**
     * 数组转字符串
     * @param array
     * @param separator
     * @return
     */
    public static String join(Object[] array,String separator) {
        if (array == null || array.length == 0) {
            return "";
        }
        int length = array.length;
        separator = getNString(separator);
        StringBuffer sb = new StringBuffer(100);
        for (int i = 0;i <length; i++) {
            sb.append(getNString(array[i])).append(separator);
        }
        return subString(sb.toString(),0, sb.toString().length() - separator.length());
    }

    /**
     * 判断是否空白
     * @param str
     * @return
     */
    public static boolean isBlank(String str){
        if (isEmpty(getNString(str))){
            return true;
        }
        return false;
    }

    /**
     * 判断是否非空白
     * @param str
     * @return
     */
    public static boolean isNotBlank(String str){
        return !(isBlank(str));
    }
    public static void main(String[] args) {
        //System.out.println(getRandonStr(64));
    }

    /**
     * @Title: getPackageByPath
     * @Description 通过指定文件获取类全名
     * @param classFile 类文件
     * @return String 类全名
     */
    public static String getPackageByPath(File classFile, String exclude){
        if(classFile == null || classFile.isDirectory()){
            return null;
        }

        String path = classFile.getAbsolutePath().replace('\\', '/');

        path = path.substring(path.indexOf(exclude) + exclude.length()).replace('/', '.');
        if(path.startsWith(".")){
            path = path.substring(1);
        }
        if(path.endsWith(".")){
            path = path.substring(0, path.length() - 1);
        }

        return path.substring(0, path.lastIndexOf('.'));
    }
}
